'''
Author: xialang333@gmail.com
Date: 2024-08-19 15:31:39
LastEditTime: 2024-08-19 17:07:35
# devops_servers表添加数据

'''
import ipaddress

from common import ConnDB, CustomLogger

# host_type: 1 为容器、2为宿主机
host_type = 1
# 增加的数量
host_num = 3
# 增加数据的起始ip
start_ip = "10.28.4.150"
# 创建并配置日志记录器
logger = CustomLogger('devops_servers').get_logger()

class Main:

    def run(self):
        conn = ConnDB()
        conn.connect()
        ip = ipaddress.IPv4Address(start_ip)
        for i in range(host_num):
            next_ip = ip + i + 1  # 加 1 是因为从下一个 IP 开始计数
            host = str(next_ip)
            co_name = "host-"+ host
            sql = f"INSERT INTO `devops_swat`.`devops_servers`(`ip`, `ip_pub`, `os_version`, `room`, `ssh_port`, `physical_id`, `type`, `docker_id`, `docker_cmd`, `status`, `profile`, `remark`, `modified`, `modifier`, `yn`) VALUES ('{host}', '10.2.16.238', 'image.jd.local/yanxi-base:7.5.1804', '未知', 22,  20, {host_type}, '1961fb596ffdf35f728be1fbeadea4ecb16d2507681317fae407fe4561b0fcce', 'docker run -dit --name {co_name} --cpus=1 --memory=2g --storage-opt size=20G --network znkf_network --ip {host}--hostname={co_name} --restart always image.jd.local/yanxi-base:7.5.1804', 1, '{{\"loginType\":\"ssh\"}}', '', '2023-10-30 08:00:31', 'zhangjueqi', 1);"
            
            conn.execute_query(sql)
            logger.info(f"插入devops_servers表数据ip: {next_ip}")
        
        conn.close()
        logger.info(f"devops_servers表数据新增完成: 共计{counter}条")

if __name__ == "__main__":
    tt = Main()
    tt.run()